package com.czy.owner.ui.message;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.czy.owner.R;
import com.czy.owner.adapter.ChatHistoryAdapter;
import com.czy.owner.global.Constants;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.widget.PageTitleView;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.utils.EaseCommonUtils;
import com.hyphenate.easeui.widget.EaseAlertDialog;


import java.util.List;

import butterknife.BindView;

/**
 * Created by King on 2016/6/23 0023.
 */
public class ChatHistoryActivity extends BaseActivity implements View.OnClickListener {
    private final String TAG = getClass().getSimpleName();

    private PageTitleView pageTitle;
    @BindView(R.id.lv_chat_history)
    ListView lvHistory;
    @BindView(R.id.btn_chat_history_first)
    Button btnFirst;
    @BindView(R.id.btn_chat_history_previous)
    Button btnPrevious;
    @BindView(R.id.btn_chat_history_next)
    Button btnNext;
    @BindView(R.id.btn_chat_history_last)
    Button btnLast;
    @BindView(R.id.btn_chat_history_delete)
    Button btnDelete;

    private String groupId;
    private EMConversation conversation;
    private List<EMMessage> allMessagesData, showMessagesData;
    private int pageIndex = 0, pageSize = 20, pageCount = 0, msgCount = 0;
    private ChatHistoryAdapter mAdapter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_chat_history;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        if (getIntent().getExtras() == null) {
            Toast.makeText(this, "数据异常",Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
        groupId = getIntent().getExtras().getString(Constants.TRANSMISSION_CONTENT_KEY);
        conversation = EMClient.getInstance().chatManager().getConversation(groupId, EaseCommonUtils.getConversationType(EaseConstant.CHATTYPE_GROUP), true);
        msgCount = conversation.getAllMsgCount();
        if (msgCount > 1000) {
            msgCount = 1000;
        }
        pageTitle = new PageTitleView((RelativeLayout) findViewById(R.id.rl_page_title), "聊天记录(0/0)", true);
        if (msgCount == 0) {
            pageCount = 0;
        } else {
            pageCount = msgCount / pageSize;
            if (msgCount % pageSize > 0) {
                pageCount++;
            }
            pageIndex = 1;
        }
        allMessagesData = conversation.loadMoreMsgFromDB(null, 1000);
        initListener();
        initButton();
        initView();
    }

    @Override
    protected void updateViews() {

    }


    private void initListener() {
        pageTitle.getLeftButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btnFirst.setOnClickListener(this);
        btnPrevious.setOnClickListener(this);
        btnNext.setOnClickListener(this);
        btnLast.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
    }

    private void initButton() {
        if (pageIndex < pageCount) {
            btnFirst.setEnabled(true);
            btnPrevious.setEnabled(true);
        } else {
            btnFirst.setEnabled(false);
            btnPrevious.setEnabled(false);
        }
        if (pageIndex > 1) {
            btnNext.setEnabled(true);
            btnLast.setEnabled(true);
        } else {
            btnNext.setEnabled(false);
            btnLast.setEnabled(false);
        }
    }

    private void initView() {
        pageTitle.getCenterTextView().setText("聊天记录(" + pageIndex + "/" + pageCount + ")");
        if (msgCount == 0 || pageIndex == 0 || pageIndex > pageCount) {
            return;
        }
        showMessagesData = allMessagesData.subList((pageIndex - 1) * pageSize, pageIndex * pageSize > msgCount ? msgCount : pageIndex * pageSize);

        mAdapter = new ChatHistoryAdapter(this, groupId, lvHistory, showMessagesData);
        lvHistory.setAdapter(mAdapter);
        lvHistory.setSelection(showMessagesData.size() - 1);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_chat_history_first:
                pageIndex = pageCount;
                initButton();
                initView();
                break;
            case R.id.btn_chat_history_previous:
                pageIndex++;
                initButton();
                initView();
                break;
            case R.id.btn_chat_history_next:
                pageIndex--;
                initButton();
                initView();
                break;
            case R.id.btn_chat_history_last:
                pageIndex = 1;
                initButton();
                initView();
                break;
            case R.id.btn_chat_history_delete:
                String msg = getResources().getString(R.string.Whether_to_empty_all_chats);
                new EaseAlertDialog(ChatHistoryActivity.this, null, msg, null, new EaseAlertDialog.AlertDialogUser() {

                    @Override
                    public void onResult(boolean confirmed, Bundle bundle) {
                        if (confirmed) {
                            EMClient.getInstance().chatManager().deleteConversation(groupId, true);
                            finish();
                        }
                    }
                }, true).show();
                break;
        }
    }
}
